<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-09-02 16:38
 *
 * 项目：rm  -  $  - run.php
 *
 * 作者：liwei 
 */

use modules\levpays\sdk\wechatpay\h5\H5;
use modules\levpays\sdk\wechatpay\InstancePay;
use modules\levpays\sdk\wechatpay\jsapi\Jsapi;
use modules\levpays\sdk\wechatpay\native\Native;

$code = Lev::stripTags(Lev::GPv('code'));
$openid = Lev::opCookies('wxopenid');

/* @var $goods array */
?>

<?php if (Lev::checkWxUserAgent()): $pre = Jsapi::tradePre();?>
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
    <script>
        (function () {
            'use strict';

            jQuery(function () {

                levpaysPay.pre = '<?=$pre?>';

                levpaysPay.levpaysPaySubmit = function() {
                    levpaysPay.pre = '<?=$pre?>';


                <?php if (InstancePay::isWxmini()): $openid = Lev::stripTags(Lev::GPv('openid'));?>
                    wxminiPay();
                    <?php else:?>
                    if (typeof WeixinJSBridge == "undefined") {//levtoast(123123);
                        if (document.addEventListener) {
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                        } else if (document.attachEvent) {
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                        }

                        window.setTimeout(function () {wxminiPay();},  800);
                    } else {//levtoast(444);
                        onBridgeReady();
                    }
                    <?php endif;?>

                }

                function onBridgeReady() {
                    Levme.ajaxv.getv(levToRoute('pay/pay-sign', {id:'levpays', openid:'<?=$openid?>', code:'<?=$code?>', tradeId:'<?=$goods['tradeId']?>',inajax:1}),
                        function (data, status) {//myApp.alert('<div style="overflow: auto" ">'+JSON.stringify(data)+'</div>',1000000);
                            if (data.authurl) {
                                window.location = data.authurl;
                                return;
                            }
                            WeixinJSBridge.invoke('getBrandWCPayRequest', data.paySign, function (res) {//myApp.alert(JSON.stringify(res),100000);
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
                                    // 使用以上方式判断前端返回,微信团队郑重提示：
                                    //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                                }
                            });
                        })
                }

                function wxminiPay() {
                    window.setTimeout(function () {
                        wx.miniProgram.getEnv(function (res) {
                            wx.miniProgram.navigateTo({url: '/pages/pay/index?webv=1&tradeId=<?=$goods['tradeId']?>'});
                            if (!res.miniprogram) {
                                levtoast('抱歉，请使用小程序打开')
                            }
                        });
                        window.__wxjs_environment === 'miniprogram' || levtoast('抱歉，请在微信中打开');
                    },  800);
                }

                <?=$code ? 'levpaysPay.levpaysPaySubmit();' : ''?>
            });

            jQuery('.paytypeidbox-1').hide();
            //jQuery('.paytypeidbox-0 input[name=paytypeid]').click();
        })();
    </script>
<?php elseif (Lev::ckmobile()): $pre = H5::tradePre();?>
<script>
    jQuery(function () {
        'use strict';

        levpaysPay.pre = '<?=$pre?>';

        jQuery(function () {

            levpaysPay.setPaytypeidInit[0] = function () {
                levpaysPay.pre = '<?=$pre?>';

                var url = '<?=H5::PlaceOrder($goods['tradeNo'], $goods['name'], $goods['paymoney']);?>';
                jQuery('.levpaysPaySubmitBtn').html('<a class="flex-box ju-sa color-white" href="'+url+'" target="_blank" _bk=1>前往微信支付</a>');
            };
            //levpaysPay.setPaytypeidInit[0]();

            levpaysPay.levpaysPaySubmit = function() {
                levpaysPay.pre = '<?=$pre?>';

            }
        });

    });
 </script>
<?php else: $pre = Native::tradePre();?>

    <?=\lev\base\Assetsv::qrcodeJs(1)?>

<script>

    jQuery(function () {
        levpaysPay.pre = '<?=$pre?>';

        levpaysPay.setPaytypeidInit[0] = function () {
            levpaysPay.pre = '<?=$pre?>';

            jQuery('.levpaysPaySubmitBtn').addClass('disabled').html('请扫码支付');
            setLevpaysQrcode();
        };

        levpaysPay.levpaysPaySubmit = function() {
            levpaysPay.pre = '<?=$pre?>';

            Levme.showNotices('请使用微信扫码支付');
            setLevpaysQrcode();
        }

        setLevpaysQrcode();
        function setLevpaysQrcode() {
            jQuery('#levpays-qrcode').html('').removeClass('hiddenx');
            jQuery('.levpays-qrcode-box').removeClass('hiddenx');
            new QRCode('levpays-qrcode', {
                text: '<?=Native::PlaceOrder($goods['tradeNo'], $goods['name'], $goods['paymoney']);?>',
                width: 148,
                height: 148,
                colorDark: "#000000",
                colorLight: "#ffffff",
                correctLevel: QRCode.CorrectLevel.H
            });
        }
    });

</script>
<?php endif;?>

